package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baor.dao.HolderDao;
import com.baor.dao.MonthlyDataDao;
import com.baor.domain.HolderEquipmentVO;
import com.baor.domain.MonthlyDataVo;
import com.baor.service.HolderService;
import com.baor.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class HolderServiceImpl implements HolderService {

    @Autowired
    private HolderDao holderDao;
    @Autowired
    private MonthlyDataDao monthlyDataDao;
    @Override
    public List<HolderEquipmentVO> getHolderEquipments(Map<String, Object> params) {
        validateRequiredParam(params, "customerId");
        Integer customerId = convertToInteger(params.get("customerId"), "customerId");
        return holderDao.findHolderEquipmentStats(customerId);
    }
    private void validateRequiredParam(Map<String, Object> params, String paramName) {
        if (params == null || !params.containsKey(paramName) || params.get(paramName) == null) {
            throw new IllegalArgumentException("缺少必要参数: " + paramName);
        }
        String value = params.get(paramName).toString();
        if (!StringUtils.hasText(value)) {
            throw new IllegalArgumentException("参数不能为空: " + paramName);
        }
    }

    private Integer convertToInteger(Object value, String paramName) {
        try {
            return Integer.valueOf(value.toString());
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("参数格式错误: " + paramName + " 必须是整数");
        }
    }
    @Override
    public R monthlyData(Map<String, Object> params) {
        Integer holderId = (Integer) params.get("holderId");
        LambdaQueryWrapper<MonthlyDataVo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MonthlyDataVo::getHolderId, holderId);
        List<MonthlyDataVo> list = this.monthlyDataDao.selectList(queryWrapper);

        return R.ok(list);
    }
}